// import { login, logout, getInfo } from '@/api/user'
import { getToken, setToken, removeToken } from '@/utils/auth'
// import { resetRouter } from '@/router'
import { login, getUserInfo, getMoreInfo } from '@/api/user'
const state = {
  token: getToken() || null,
  userInfo: null
}

const mutations = {
  setToken(state, payload) {
    state.token = payload.token
  },
  setUserInfo(state, payload) {
    state.userInfo = payload
  }
}

const actions = {
  async addToken(context, payload) {
    const token = await login(payload)
    context.commit('setToken', { token })
    setToken(token)
  },
  async getUserInfoAction(context) {
    const res = await getUserInfo()
    // console.log('res', res)
    const res2 = await getMoreInfo(res.userId)
    // console.log(res2)
    context.commit('setUserInfo', { ...res, ...res2 })
  },
  logout(context) {
    context.commit('setUserInfo', null)
    removeToken()
    context.commit('setToken', { token: null })
    // context.userInfo = null
    // context.token = null
    // removeToken()
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
